#include "Box.hh"
#include "utils.hh"

GLfloat diff[] = {0.5, 0.5, 0.7}; 
GLfloat amb[] = {0.05, 0.05, 0.05};
GLfloat specular[] = {0.7, 0.7, 0.7};

Box::Box(float x, float y, float z) :
	slPrimitive()
{
	glEnable(GL_COLOR_MATERIAL);
	btVector3 	localInertia(0, 0, 0);
	shape = new btBoxShape( btVector3(1,1,1) );
	myTransform.setIdentity();
	myTransform.setOrigin( btVector3(x,y,z) );
	mass = 0.5f;
	if (mass)
		shape->calculateLocalInertia(mass, localInertia);
	myMotionState = new btDefaultMotionState(myTransform);
	btRigidBody::btRigidBodyConstructionInfo myBoxRigidBodyConstructionInfo( mass, myMotionState, shape, localInertia );
	body = new btRigidBody(myBoxRigidBodyConstructionInfo);
	_r = 1;
	_g = 1;
	_b = 1;
	id = 0;
	texture = false;
}

void 			Box::enableTexture()
{
	texture = true;
}

void 			Box::disableTexture()
{
	texture = false;
}

void 			Box::setTexture(GLuint textureID)
{
	id = textureID;
}


void Box::draw()
{
	glEnable(GL_COLOR_MATERIAL);
	myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(matrix);
	glPushMatrix();
	glMultMatrixf(matrix);
	glColor3f(_r,  _g, _b);

	GLfloat a = 0.7;

	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &a);

	//if (texture)
	//	glBindTexture(GL_TEXTURE_2D, id);
	glBegin (GL_QUADS);

	

	glNormal3f(0.0f, 0.0f, -1.0f); // Normal face derriere
	glTexCoord2i(0,0);	glVertex3i(-1,-1,-1);
	glTexCoord2i(1,0);	glVertex3i(+1,-1,-1);
	glTexCoord2i(1,1);	glVertex3i(+1,+1,-1); 
	glTexCoord2i(0,1);	glVertex3i(-1,+1,-1);

	glNormal3f(0.0f, 0.0f, 1.0f); // Normal face devant
	glTexCoord2i(0,0);	glVertex3i(-1,-1,+1);
	glTexCoord2i(1,0);	glVertex3i(+1,-1,+1); 
	glTexCoord2i(1,1);	glVertex3i(+1,+1,+1); 
	glTexCoord2i(0,1);	glVertex3i(-1,+1,+1);

	glNormal3f(1.0f, 0.0f, 0.0f); // Normal face droite
	glTexCoord2i(0,0);	glVertex3i(+1,-1,-1); 
	glTexCoord2i(1,0);	glVertex3i(+1,-1,+1);
	glTexCoord2i(1,1);	glVertex3i(+1,+1,+1);
	glTexCoord2i(0,1);	glVertex3i(+1,+1,-1);


	glNormal3f(-1.0f, 0.0f, 0.0f); // Normal face gauche
	glTexCoord2i(0,0);	glVertex3i(-1,-1,-1); 
	glTexCoord2i(1,0);	glVertex3i(-1,-1,+1); 
	glTexCoord2i(1,1);	glVertex3i(-1,+1,+1);
	glTexCoord2i(0,1);	glVertex3i(-1,+1,-1);


	glNormal3f(0.0f, 1.0f, 0.0f); // Normal face haut
	glTexCoord2i(1,0);	glVertex3i(-1,+1,-1); 
	glTexCoord2i(1,1);	glVertex3i(+1,+1,-1); 
	glTexCoord2i(0,1);	glVertex3i(+1,+1,+1);
	glTexCoord2i(0,0);	glVertex3i(-1,+1,+1);

	glNormal3f(0.0f, -1.0f, 0.0f); // Normal face bas
	glTexCoord2i(1,0);	glVertex3i(-1,-1,+1); 
	glTexCoord2i(1,1);	glVertex3i(+1,-1,+1);
	glTexCoord2i(0,1);	glVertex3i(+1,-1,-1);
	glTexCoord2i(0,0);	glVertex3i(-1,-1,-1);


	glEnd();


	glPopMatrix();
}

Box::~Box()
{

}